package fun.xinnix.controller;

import fun.xinnix.entity.User;
import fun.xinnix.repo.UserRepo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserRepo userRepo;

    @GetMapping("/query")
    public List<User> query(User user) {
        return userRepo.findByName(user.getName());
    }
    @GetMapping("/{id}")
    public User getUser(@PathVariable("id") Long id) {
        Optional<User> optionalUser = userRepo.findById(id);
        return optionalUser.orElse(null);
    }

    @PostMapping
    public User add(@RequestBody User user) {
        return userRepo.save(user);
    }

    @DeleteMapping("{id}")
    public void del(@PathVariable("id") Long id) {
        userRepo.deleteById(id);
    }
}
